package com.qf.helloworld;

import com.qf.util.ConnectionUtil;
import com.rabbitmq.client.*;
import org.junit.Test;

import java.io.IOException;
import java.util.Scanner;

/**
 * 消费者
 * @author zjw
 */
public class Consumer {

    @Test
    public void consume() throws Exception {
        //1. 创建连接对象
        Connection connection = ConnectionUtil.getConnection();

        //2. 构建Channel
        Channel channel = connection.openChannel().get();

        //3. 监听hello_queue队列,并设置回调函数
        DefaultConsumer callback = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者接收到消息：" + new String(body));
            }
        };
        channel.basicConsume("hello_queue",true,callback);
        System.out.println("开始监听！");
        //4. System.in.
        System.in.read();

        System.out.println("凉凉~~");
        //5. 释放资源
        channel.close();
        connection.close();
    }
}
